const path = require('node:path')

module.exports = (codeConfig, utils) => {
  const { listPageApiConfig, hasModifyDialog, addApiConfig, modifyApiConfig } =
    codeConfig.apiConfigStates

  const modifyFormDataExtends = []
  if (addApiConfig?.request.schema.name) {
    modifyFormDataExtends.push(addApiConfig.request.schema.name)
  }
  if (modifyApiConfig?.request.schema.name) {
    modifyFormDataExtends.push(modifyApiConfig.request.schema.name)
  }
  const code = `
    /** 页面查询Query参数类型 */
    export interface Query extends  Partial<${listPageApiConfig.request.schema.name}> {}

    /** 页面表格Row类型 */
    export interface TableRow extends ${utils.extractGenericType(
      listPageApiConfig.response.schema.name
    )} {}

    ${
      hasModifyDialog ?
        `
          /** 新增编辑表单formData类型 */
          export interface ModifyFormData extends Partial<${modifyFormDataExtends.join('&')}> {}
        ` :
        ''
    }
  `
  utils.writeFile(path.join(codeConfig.modulePath, 'types', 'extends.ts'), code)
}
